﻿namespace WMS6.Web.Outbound
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.OutboundServiceRef;
    using WMS6.Web.UserControls;

    public class OrderSelectionList : BasePage
    {
        protected Button btnAction;
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F7022;
        protected GridView gvOrderSelection;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRtnValue;
        protected HtmlHead Head1;
        protected Label lblMsg;
        protected Label lblUom;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2OrderSelectionList(OrderSelectionQueryEntity OrderSelectionQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(OrderSelectionQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = OutboundServiceClientProxy.GetInstance().GetOrderSelectionByQueryEntity(OrderSelectionQuery);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                base.BindData2GridView(this.gvOrderSelection, ds);
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvOrderSelection, "ORDER_SELECTION_ID");
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7200_009");
                }
                else
                {
                    this.lblMsg.Text = OutboundServiceClientProxy.GetInstance().DeleteOrderSelectionInfos(selectedID) + base.GetGlobalMsgResource("F7200_007");
                    OrderSelectionQueryEntity orderSelectionQuery = this.GetOrderSelectionQueryEntity();
                    this.BindData2OrderSelectionList(orderSelectionQuery);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7200_008") + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> OrderID = base.GetSelectedIDsInGridView(this.gvOrderSelection, "ORDER_SELECTION_ID");
            if ((OrderID == null) || (OrderID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7200_009");
            }
            else if (OrderID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7200_010");
            }
            else
            {
                base.Response.Redirect(string.Format("OrderSelection.aspx?from=list&ID={0}", base.UrlEncode(OrderID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            OrderSelectionQueryEntity orderSelectionQuery = this.GetOrderSelectionQueryEntity();
            orderSelectionQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = orderSelectionQuery.CurrentPage;
            orderSelectionQuery.CurrentPage = "0";
            this.gvOrderSelection.Columns[1].Visible = false;
            this.gvOrderSelection.AllowSorting = false;
            this.BindData2OrderSelectionList(orderSelectionQuery);
            this.hdnActionID.Value = "";
            this.gvOrderSelection.ShowFooter = false;
            orderSelectionQuery.IsGetAll = false;
            orderSelectionQuery.CurrentPage = currentPageIndex;
            this.SetOrderSelectionQueryEntity(orderSelectionQuery);
            base.Export2Excel("OrderSelection.xls", this.gvOrderSelection);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            OrderSelectionQueryEntity orderSelectionQuery = this.GetOrderSelectionQueryEntity();
            orderSelectionQuery.CurrentPage = "0";
            this.BindData2OrderSelectionList(orderSelectionQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            OrderSelectionQueryEntity orderSelectionQuery = this.GetOrderSelectionQueryEntity();
            orderSelectionQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2OrderSelectionList(orderSelectionQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            OrderSelectionQueryEntity orderSelectionQuery = this.GetOrderSelectionQueryEntity();
            orderSelectionQuery.OrderSelectionID = "";
            orderSelectionQuery.EnablePacking = "";
            orderSelectionQuery.MaxOrders = "";
            orderSelectionQuery.CartonGroup = "";
            orderSelectionQuery.BatchPickMaxCount = "";
            orderSelectionQuery.BatchPickMaxCube = "";
            orderSelectionQuery.IsDefault = "";
            this.BindData2OrderSelectionList(orderSelectionQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            OrderSelectionQueryEntity orderSelectionQuery = this.GetOrderSelectionQueryEntity();
            if (int.Parse(orderSelectionQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                orderSelectionQuery.CurrentPage = (int.Parse(orderSelectionQuery.CurrentPage) + 1).ToString();
                this.BindData2OrderSelectionList(orderSelectionQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            OrderSelectionQueryEntity orderSelectionQuery = this.GetOrderSelectionQueryEntity();
            if (int.Parse(orderSelectionQuery.CurrentPage) > 0)
            {
                orderSelectionQuery.CurrentPage = (int.Parse(orderSelectionQuery.CurrentPage) - 1).ToString();
                this.BindData2OrderSelectionList(orderSelectionQuery);
            }
        }

        private OrderSelectionQueryEntity GetOrderSelectionQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as OrderSelectionQueryEntity);
            }
            return (base.PageQueryEntitySession as OrderSelectionQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                OrderSelectionQueryEntity orderSelectionQuery = this.GetOrderSelectionQueryEntity();
                orderSelectionQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2OrderSelectionList(orderSelectionQuery);
            }
        }

        protected void gvOrderSelection_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F7022", this.gvOrderSelection);
        }

        protected void gvOrderSelection_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvOrderSelection_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string strValue = base.UrlReplace(this.gvOrderSelection.DataKeys[e.NewSelectedIndex]["ORDER_SELECTION_ID"].ToString());
            if (!string.IsNullOrEmpty(strValue))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'> window.returnValue ='" + strValue + "';  window.close();</script>", false);
            }
        }

        protected void gvOrderSelection_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                OrderSelectionQueryEntity orderSelect = this.GetOrderSelectionQueryEntity();
                orderSelect.SortField = e.SortExpression;
                if (orderSelect.SortDirection == "ASC")
                {
                    orderSelect.SortDirection = "DESC";
                }
                else if (orderSelect.SortDirection == "DESC")
                {
                    orderSelect.SortDirection = "ASC";
                }
                this.BindData2OrderSelectionList(orderSelect);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            OrderSelectionQueryEntity orderSelectionQuery = new OrderSelectionQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    orderSelectionQuery.OrderSelectionID = this.PreviousPage.OrderSelectionID;
                    orderSelectionQuery.EnablePacking = this.PreviousPage.EnablePacking;
                    orderSelectionQuery.MaxOrders = this.PreviousPage.MaxOrders;
                    orderSelectionQuery.CartonGroup = this.PreviousPage.CartonGroup;
                    orderSelectionQuery.BatchPickMaxCount = this.PreviousPage.BatchPickMaxCount;
                    orderSelectionQuery.BatchPickMaxCube = this.PreviousPage.BatchPickMaxCube;
                    orderSelectionQuery.IsDefault = this.PreviousPage.IsDefault;
                }
                else
                {
                    orderSelectionQuery.OrderSelectionID = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            orderSelectionQuery.IsGetAll = false;
            orderSelectionQuery.CurrentPage = "0";
            orderSelectionQuery.PageSize = this.gvOrderSelection.PageSize.ToString();
            orderSelectionQuery.SortField = "ORDER_SELECTION_ID";
            orderSelectionQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvOrderSelection.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"].ToString();
                this.gvOrderSelection.Columns[1].Visible = false;
                this.gvOrderSelection.Columns[2].Visible = true;
                this.btnNew.Visible = false;
                this.btnEdit.Visible = false;
                this.btnDelete.Visible = false;
                this.btnReSearch.Visible = false;
                this.btnExport.Visible = false;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetOrderSelectionQueryEntity(orderSelectionQuery);
            this.BindData2OrderSelectionList(orderSelectionQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetOrderSelectionQueryEntity(OrderSelectionQueryEntity orderSelectionQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = orderSelectionQuery;
            }
            else
            {
                base.PageQueryEntitySession = orderSelectionQuery;
            }
        }

        public OrderSelectionQuery PreviousPage
        {
            get
            {
                return (OrderSelectionQuery) base.PreviousPage;
            }
        }
    }
}

